<script setup lang="ts">
import {
  deleteProductReturnApi,
  getProductReturnListApi,
} from "@/api/ProductReturn";
import { ProductReturnResultsData } from "@/api/ProductReturn/res";
import {
  ProductReturnSearchType,
  PaginationType,
} from "@/api/ProductReturn/params";
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

// mockData
let procedureData = ref(["工序一", "工序二", "工序三"]);
let loading = ref(false);
let ifSearch = ref(false);
let params = ref<PaginationType>({
  currentPage: 1,
  pageSize: 10,
});
let searchData = ref<ProductReturnSearchType>({
  returnListId: "",
  workListId: "",
  returnDate: "",
  taskId: "",
  craftName: "",
});
let flowData = ref([
  "工艺流程一",
  "工艺流程二",
  "工艺流程三",
  "工艺流程四",
  "工艺流程五",
]);
let tableData = ref<ProductReturnResultsData[]>([
  {
    returnListId: "",
    getMaterialNum: "",
    objectId: "",
    craftLine: "",
    workListId: "",
    workListName: "",
    returnPeople: "",
    returnListName: "",
    materialInfo: "",
    craftName: "",
    returnDate: "",
    taskName: "",
    taskId: "",
  },
]);
let total = ref(0);
let showDatePicker = ref(false);
const filteredSearchData: Partial<ProductReturnSearchType> = {};
const $router = useRouter();

const handler = (page) => {
  params.value.currentPage = page;
  if (ifSearch.value) {
    getTableData(filteredSearchData);
  } else {
    getTableData();
  }
};
const sizeHandler = (page_size) => {
  params.value.pageSize = page_size;
  if (ifSearch.value) {
    getTableData(filteredSearchData);
  } else {
    getTableData();
  }
};
const gotoAdd = () => {
  $router.push("/storage/entrance/addReturn");
};
const gotoDetail = () => {
  $router.push("/storage/entrance/returnDetail");
};
const handleClick = () => {};

const getTableData = async (data?) => {
  try {
    loading.value = true;
    const TotalRes = await getProductReturnListApi();
    total.value = TotalRes.results.length;
    const res = await getProductReturnListApi(data,params.value);
    tableData.value = res.results;
    if (ifSearch.value) {
      total.value = res.results.length;
    }
  } catch (err: any) {
    console.log(err);
    ElMessage.error("获取数据失败");
  } finally {
    loading.value = false;
  }
};

const updateHandler = (objectId: string) => {
  $router.push({
    path: "/storage/entrance/updateReturn",
    query: { objectId },
  });
};

const deleteData = async (objectId: string) => {
  try {
    await deleteProductReturnApi(objectId);
    getTableData();
    ElMessage.success("删除成功");
  } catch (err: any) {
    console.log(err);
    ElMessage.error("删除失败");
  }
};

const onSearch = () => {
  let allFieldsEmpty = true;
  Object.keys(filteredSearchData).forEach((key) => {
    delete filteredSearchData[key];
  });
  for (const key in searchData.value) {
    if (searchData.value[key] !== "" && searchData.value[key] !== null) {
      allFieldsEmpty = false;
      filteredSearchData[key] = searchData.value[key];
    }
  }
  ifSearch.value = !allFieldsEmpty;
  params.value.currentPage = 1;
  params.value.pageSize = 10;
  getTableData(filteredSearchData);
};

const reset = () => {
  searchData.value = {
    returnListId: "",
    workListId: "",
    returnDate: "",
    taskId: "",
    craftName: "",
  };
  params.value.currentPage = 1;
  params.value.pageSize = 10;
  ifSearch.value = false;
  getTableData();
};

onMounted(() => {
  getTableData();
});
</script>

<template>
  <div style="padding: 20px">
    <el-card>
      <div class="operateContainer">
        <div
          class="formContainer"
          :style="showDatePicker ? 'height:85px' : 'height:32px'"
        >
          <el-form inline :model="searchData">
            <el-form-item prop="returnListId">
              <el-input
                placeholder="请输入退料单编号/名称"
                style="width: 250px"
                v-model="searchData.returnListId"
              ></el-input>
            </el-form-item>
            <el-form-item prop="workListId">
              <el-input
                placeholder="请输入工单编号/名称"
                style="width: 250px"
                v-model="searchData.workListId"
              ></el-input>
            </el-form-item>
            <el-form-item prop="returnDate">
              <el-date-picker
                style="width: 250px"
                type="date"
                placeholder="请选择退料日期"
                v-model="searchData.returnDate"
              ></el-date-picker>
            </el-form-item>
            <div style="margin-top: 20px">
              <el-form-item prop="taskId">
                <el-input
                  placeholder="请输入任务编号/名称"
                  style="width: 250px"
                  v-model="searchData.taskId"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-input
                  placeholder="请输入来料检验单编号/名称"
                  style="width: 250px"
                ></el-input>
              </el-form-item>
              <el-form-item prop="craftName">
                <el-select
                  v-model="searchData.craftName"
                  placeholder="请选择工序"
                  style="width: 250px"
                >
                  <el-option
                    v-for="item in procedureData"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-select placeholder="请选择工艺流程" style="width: 250px">
                  <el-option
                    v-for="item in flowData"
                    :key="item"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-form>
        </div>
        <div class="searchOperate">
          <div class="button">
            <el-button type="primary" plain style="width: 80px" @click="reset"
              >重置</el-button
            >
            <el-button type="primary" style="width: 80px" @click="onSearch"
              >查询</el-button
            >
          </div>
          <div class="more">
            <el-button
              type="primary"
              link
              @click="showDatePicker = !showDatePicker"
              >更多</el-button
            >
            <el-icon color="#1B9AEE" :class="{ 'rotate-180': showDatePicker }">
              <CaretBottom />
            </el-icon>
          </div>
        </div>
      </div>
    </el-card>
    <el-card style="margin-top: 20px">
      <template #header>
        <span style="font-weight: bold">生产退料</span>
      </template>
      <div class="operate">
        <el-button style="width: 80px" type="primary" @click="gotoAdd"
          >新增</el-button
        >
        <!-- <el-button style="width: 80px" type="primary" plain>删除</el-button> -->
      </div>
      <el-table
        :data="tableData"
        style="margin-top: 15px"
        border
        v-loading="loading"
      >
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column
          align="center"
          label="序号"
          type="index"
          width="60"
          fixed="left"
        ></el-table-column>
        <el-table-column
          align="center"
          label="退料单名称"
          width="100"
          fixed="left"
          prop="returnListName"
        >
          <template #="{ row, $index }">
            <el-button link type="primary" @click="gotoDetail">{{
              row.returnListName
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="退料单编号"
          width="110"
          fixed="left"
          prop="returnListId"
        ></el-table-column>
        <el-table-column
          align="center"
          label="退料日期"
          width="100"
          prop="returnDate"
        ></el-table-column>
        <el-table-column
          align="center"
          label="退料人"
          prop="returnPeople"
          width="125"
        ></el-table-column>
        <el-table-column
          align="center"
          label="工单名称"
          width="90"
          prop="workListName"
        ></el-table-column>
        <el-table-column
          align="center"
          label="工单编号"
          width="110"
          prop="workListId"
        ></el-table-column>
        <el-table-column
          align="center"
          label="任务名称"
          width="90"
          prop="taskName"
        ></el-table-column>
        <el-table-column
          align="center"
          label="任务编号"
          width="90"
          prop="taskId"
        ></el-table-column>
        <el-table-column
          align="center"
          label="工艺路线"
          width="100"
          prop="craftLine"
        ></el-table-column>
        <el-table-column
          align="center"
          label="工序名称"
          width="90"
          prop="craftName"
        ></el-table-column>
        <el-table-column
          align="center"
          label="物料信息"
          width="100"
          prop="materialInfo"
        ></el-table-column>
        <el-table-column
          align="center"
          label="领料总数"
          width="90"
          prop="getMaterialNum"
        ></el-table-column>
        <el-table-column
          align="center"
          label="备注"
          width="160"
          prop="remark"
        ></el-table-column>
        <el-table-column align="center" label="操作" width="200" fixed="right">
          <template #="{ row, $index }">
            <el-button link type="primary" @click="handleClick">
              出库
            </el-button>
            <el-button link type="primary" @click="updateHandler(row.objectId)"
              >编辑</el-button
            >
            <el-button link type="primary" @click="deleteData(row.objectId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="params.currentPage"
        v-model:page-size="params.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :background="true"
        layout="prev,pager,next,jumper,->,sizes,total"
        :total="total"
        @current-change="handler"
        @size-change="sizeHandler"
        style="margin-top: 10px"
      ></el-pagination>
    </el-card>
  </div>
</template>

<style scoped lang="scss">
.operateContainer {
  display: flex;
  align-content: center;
  justify-content: space-between;
  .formContainer {
    width: 1060px;
    height: 32px;
    transition: height 0.3s ease; // 添加高度变化的过渡效果
    overflow: hidden; // 防止内容溢出
    .el-form-item {
      margin-bottom: 0;
    }
  }
  .searchOperate {
    display: flex;
    align-items: center;
    .more {
      margin-left: 20px;
      display: flex;
      align-items: center;
    }
  }
}

/* 定义旋转动画 */
.rotate-180 {
  transition: transform 0.3s ease;
  transform: rotate(180deg);
}
</style>
